﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using database;

namespace Registrar
{
    public partial class Form1 : Form
    {
        dbase db = new dbase();
        DataTable users = new DataTable();
        public bool active = false;

        public Form1()
        {
            InitializeComponent();
            button1.Click += new EventHandler(button1_Click);
            this.Load += new EventHandler(Form1_Load);
            db.process_completed += new EventHandler(db_process_completed);
            timer1.Tick += new EventHandler(timer1_Tick);
            reportCardsToolStripMenuItem.Click += new EventHandler(reportCardsToolStripMenuItem_Click);
        }

        void reportCardsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4 frm4 = new Form4();
            frm4.ShowDialog();
        }

        void timer1_Tick(object sender, EventArgs e)
        {
            if (active == false)
            {
                timer1.Stop();
                panel1.Visible = true;
                menuStrip1.Visible = false;
            }
        }
        void db_process_completed(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                EventHandler eh = new EventHandler(db_process_completed);
                this.Invoke(eh, new object[] { db.data() });
                return;
            }
            users = db.data();
        }
        void Form1_Load(object sender, EventArgs e)
        {
            db.GenerateConnection("SELECT * FROM users");
        }
        void button1_Click(object sender, EventArgs e)
        {
            if (textBoxUsername.Text != "" && textBoxPassword.Text != "")
            {
                for (int i = 0; i < users.Rows.Count; i++)
                {
                    if (users.Rows[i]["fusername"].ToString() == textBoxUsername.Text
                        && users.Rows[i]["fpassword"].ToString() == textBoxPassword.Text
                        && users.Rows[i]["fposition"].ToString() == "registrar")
                    {
                        textBoxUsername.Text = "";
                        textBoxPassword.Text = "";
                        panel1.Visible = false;
                        active = true;
                        menuStrip1.Visible = true;
                        timer1.Start();
                        Form2 frm2 = new Form2(this);
                        frm2.MdiParent = this;
                        frm2.Show();
                        break;
                    }
                }
            }
        }

    }
}
